<template>
    <div class="app-container">
            <!--操作栏 begin-->
          <el-form :model="query" ref="query" :inline="true" label-width="68px">
            <el-form-item label="上传时间" prop="uploadTime">
              <el-date-picker
                v-model="query.uploadTime"
                type="daterange"
                value-format="yyyy-MM-dd"
                clearable
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="文件名称" prop="uploadTime">
              <el-input v-model="query.nameSearch" placeholder="文件名" class="handle-input mr10"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-upload" class="mr10" @click="uploadFiles()">文件上传</el-button>
            </el-form-item>
          </el-form>
            <!--操作栏 end-->

            <!-- table主要区域 begin -->
            <el-table
                :data="tableData"
                stripe
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column prop="id" label="ID" align="center" v-if="idShow"></el-table-column>
                <el-table-column prop="filename" width="280" label="文件名" show-overflow-tooltip></el-table-column>
                <!--<el-table-column label="预览" align="center" prop="view" >
                  <template slot-scope="scope" >
                    <el-image
                      v-if="scope.row.fileFormat==='.JPG'||scope.row.fileFormat==='.jpg'"
                      :src="scope.row.filePath"
                      fit="cover"
                      :style="`width:80px;height:60px;`"
                      :preview-src-list="[scope.row.filePath]"
                    >
                      <div slot="error" class="image-slot">
                        <i class="el-icon-picture-outline"></i>
                      </div>
                    </el-image>
                    <div v-if="scope.row.fileFormat==='.MP4'||scope.row.fileFormat==='.mp4'">
                      <el-button @click="viewMp4(scope.row.filePath)">观 看</el-button>
                    </div>
                    &lt;!&ndash;<vueVideoPlayer :v-if="scope.row.fileFormat==='.MP4'||scope.row.fileFormat==='.mp4'" :src="scope.row.filePath"/>&ndash;&gt;
                  </template>
                </el-table-column>-->
                <el-table-column prop="type"  width="150" label="文件类型" show-overflow-tooltip align="center"></el-table-column>
                <el-table-column prop="totalSizeName"  width="130" label="文件大小" align="right"></el-table-column>
                <el-table-column prop="location" label="location" align="center" v-if="idShow"></el-table-column>
                <el-table-column prop="identifier" label="identifier" align="center" v-if="idShow"></el-table-column>
                <el-table-column prop="uploadBy" width="130" label="上传人" align="center"></el-table-column>
                <el-table-column prop="uploadTimeString" width="155" label="上传时间"></el-table-column>
                <el-table-column label="操作" width="150" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-download"
                            class="blue"
                            @click="handleDownload(scope.$index, scope.row)"
                        >下载</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- table主要区域 end -->

            <!-- 分页 begin -->
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
            <!-- 分页 end -->

        <!--上传附件弹出框 -->
        <el-dialog v-dialogDrag title="文件上传" center :visible.sync="uploadVisible" width="60%" @close="handlerClose">
            <UploadBigFile class="uploadSlot"></UploadBigFile>
        </el-dialog>
    </div>
</template>

<script>
import {selectFileList,deleteFile} from "@/api/fastloader/uploadFile";
import UploadBigFile from './Upload';

export default {
    name: 'basetable',
    data() {
        return {
            query: {
                uploadTime:'',
                nameSearch: '',
                pageIndex: 1,
                //默认每页显示5条记录
                pageSize: 10
            },
            idShow: false,
            tableData: [],
            multipleSelection: [],
            uploadVisible: false,
            pageTotal: 0,
            form: {},
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取文件列表
        getData() {
          if(this.query.uploadTime!=null && this.query.uploadTime.length>0){
            this.query.uploadTimeStart=this.query.uploadTime[0];
            this.query.uploadTimeEnd=this.query.uploadTime[1];
            //this.query.uploadTime = null;
          }else{
            this.query.uploadTimeStart='';
            this.query.uploadTimeEnd='';
          }
            selectFileList(this.query).then(res => {
                this.tableData = res.data.list;
                this.pageTotal = res.data.total;
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 关闭上传文件弹出框时触发
        handlerClose(){
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(async() => {
                    let result = await deleteFile(row);
                    console.log(result);
                    if(result.data > 0){
                        this.$message.success('删除成功');
                        //this.tableData.splice(index, 1);
                      this.getData();
                    }else{
                        this.$message.error('删除失败');
                    }
                }
            );
        },
        uploadFiles(){
            this.uploadVisible = true;
        },
        //下载
        async handleDownload(index, row) {
            this.loadingOverLay = this.$loading({
                lock: true,
                text: '文件生成中',
                spinner: 'el-icon-loading',
                background: 'rgba(0,0,0,0.7)'
                });
            /*var elemIF = document.createElement('iframe');
            elemIF.src = process.env.VUE_APP_BASE_API + '/fastloader/uploader/download?id='+row.id+'&filename='+row.filename+'&location='+row.location;
            elemIF.style.display ='none';
            document.body.appendChild(elemIF);*/
          let params = {id:row.id,filename:row.filename,location:row.location};
            this.download('/fastloader/uploader/download',
              params
            , row.filename);
            this.loadingOverLay.close();
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    },
    components:{
        UploadBigFile
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 1200;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.uploadSlot {
    margin: -10px 10px 10px 30px;
}
</style>
